package richbar.com.richbar.activity.product;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.common.base.BaseActivity;
import com.common.util.StringUtil;
import com.common.util.ToastUtil;
import com.okhttp.GlideUtil;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import richbar.com.richbar.R;
import richbar.com.richbar.base.BaseApplication;
import richbar.com.richbar.bean.CollectDelResult;
import richbar.com.richbar.bean.CollectDetailResult;
import richbar.com.richbar.bean.CollectResult;
import richbar.com.richbar.util.LoadingDialog;
import richbar.com.richbar.util.NetWorkUtil;
import richbar.com.richbar.util.Util;

/**
 * Created by QiYang on 2018/12/10.
 */

public class ShopIntroAc extends BaseActivity {
    @BindView(R.id.input_body)
    LinearLayout inputBody;
    @BindView(R.id.input_et)
    TextView input_et;
    @BindView(R.id.pic)
    ImageView pic;
    @BindView(R.id.shop_name)
    TextView shopName;
    @BindView(R.id.num)
    TextView num;
    @BindView(R.id.fouce)
    TextView fouce;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.tel)
    TextView tel;
    @BindView(R.id.address)
    TextView address;
    private LoadingDialog loadingDialog;
    private boolean isCollect;
    private String id;
    private String seller_collect_id;

    @Override
    public int initLayout() {
        return R.layout.ac_collect_detail;
    }

    @Override
    public void initTitle() {

    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        loadingDialog = new LoadingDialog(this,0);
    }

    @Override
    public void initData() {
        String id = getBundle().getString("id");
        getData(id);
    }

    private void getData(String id) {
        loadingDialog.show();
        NetWorkUtil.getShopInfo(BaseApplication.uid,  id,Util.md5(), new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                loadingDialog.dismiss();
                final CollectDetailResult result = MyGson.getInstance().fromJson(data, CollectDetailResult.class);
                if (checkIsOk(result)){
                    setData(result);
                }
            }
        });
    }

    private void setData(CollectDetailResult result) {
        CollectDetailResult.DataEntity.InfoEntity data = result.getData().getInfo();
        seller_collect_id = data.getColl_id();
        name.setText(data.getSeller_name());
        num.setText(data.getSeller_collect_num() + "关注");
        shopName.setText(data.getSeller_name());
        tel.setText(data.getSeller_tel());
        GlideUtil.loadImage(this,NetWorkUtil.URL + data.getSeller_logo(),pic);
        address.setText(data.getSeller_address());
        isCollect = data.getIscollect() == 1 ? true : false;
        fouce.setText(isCollect?"已关注":"关注");
        id = data.getSeller_id() + "";
    }


    @OnClick({R.id.input_body,R.id.share, R.id.fouce, R.id.look_more,R.id.close})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.input_body:
                start(ProductListAc.class, new BaseActivity.OnBundleListener() {
                    @Override
                    public Bundle onSetBundle(Bundle bundle) {
                        bundle.putString("goodsName", StringUtil.getStr(input_et));
                        return bundle;
                    }
                });
                break;
            case R.id.share:
                break;
            case R.id.fouce:
                if (isCollect){
                    unCollect();
                }else {
                    collect();
                }
                break;
            case R.id.look_more:
                start(ShopAc.class, new OnBundleListener() {
                    @Override
                    public Bundle onSetBundle(Bundle bundle) {
                        bundle.putString("id",id);
                        return bundle;
                    }
                });
                break;
            case R.id.close:
                finish();
                break;
        }
    }


    /**
     * 收藏
     */
    private void collect() {
        loadingDialog.show();
        NetWorkUtil.addCollect(BaseApplication.uid, "2", id, Util.md5(), new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                final CollectResult result = MyGson.getInstance().fromJson(data, CollectResult.class);
                if (checkIsOk(result)) {
                    loadingDialog.dismiss();
                    if (Integer.valueOf(result.getData().getColl_id()) > 0) {
                        ToastUtil.showToast(ShopIntroAc.this, "收藏成功");
                        fouce.setText("已关注");
                        seller_collect_id = result.getData().getColl_id();
                        isCollect = true;
                    } else {
                        ToastUtil.showToast(ShopIntroAc.this, "收藏失败");
                    }

                }
            }
        });
    }

    private void unCollect(){
        loadingDialog.show();
        NetWorkUtil.delCollect("2",seller_collect_id, new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                final CollectDelResult result = MyGson.getInstance().fromJson(data, CollectDelResult.class);
                if (checkIsOk(result)){
                    loadingDialog.dismiss();
                    if (result.getData().getResult() == 1){
                        ToastUtil.showToast(ShopIntroAc.this,"删除成功");
                        fouce.setText("关注");
                        isCollect = false;
                    }else {
                        ToastUtil.showToast(ShopIntroAc.this,"删除失败");
                    }
                }
            }
        });
    }

}
